//
//  RigsterViewController.m
//  Korea
//
//  Created by 杨东奇 on 15/9/2.
//  Copyright (c) 2015年 KoreaGo. All rights reserved.
//

#import "RigsterViewController.h"
#import "Header.h"
#import "LoginViewController.h"
@interface RigsterViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *userField;
@property (nonatomic, strong)UITextField *emailField;
@property (nonatomic, strong)UITextField *passField;
@property (nonatomic, strong)UITextField *surePField;
@property (nonatomic, strong)MBProgressHUD *HUD;
@end

@implementation RigsterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRGB:0xf15b6c alpha:1];
    self.navigationController.navigationBar.translucent = NO;

    self.title = @"创建账号";
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    [self creatView];
    
}

- (void)creatView{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    // 用户头像
    UIButton *userIcorn = [UIButton buttonWithType:UIButtonTypeSystem];
    userIcorn.frame = CGRectMake(BASEWIDTH * 7.275, BASEHEIGHT * 1.5, BASEWIDTH * 4.2,BASEWIDTH * 4.2);
    [userIcorn setBackgroundImage:[UIImage imageNamed:@"head@2x.png"] forState:UIControlStateNormal];
    [userIcorn addTarget:self action:@selector(userInfoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:userIcorn];
    
    // 分割线
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,CGRectGetMaxY(userIcorn.frame) + BASEWIDTH * 1.5, self.view.width - BASEWIDTH * 6, 1)];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view1];
    
    //用户名
    _userField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(view1.frame),CGRectGetMaxY(view1.frame) + BASEWIDTH - 1, CGRectGetWidth(view1.frame), BASEHEIGHT + 3)];
    _userField.placeholder = @"手机号";
    _userField.keyboardType = UIKeyboardTypeDefault;
    _userField.returnKeyType = UIReturnKeyNext;
    _userField.delegate = self;
    _userField.tag = 10001;
    _userField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.userField setTextColor:[UIColor grayColor]];
    _userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.view addSubview:self.userField];
    
    //分割线
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,CGRectGetMaxY(_userField.frame) + BASEWIDTH - 1, self.view.width - BASEWIDTH * 6, 1)];
    view2.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view2];
    
    // 邮箱
    _emailField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(view2.frame),CGRectGetMaxY(view2.frame) + BASEWIDTH - 1, CGRectGetWidth(view2.frame), BASEHEIGHT + 3)];
    _emailField.placeholder = @"验证码";
    _emailField.keyboardType = UIKeyboardTypeEmailAddress;
    _emailField.returnKeyType = UIReturnKeyNext;
    _emailField.delegate = self;
    _emailField.tag = 10002;
    _emailField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.emailField setTextColor:[UIColor grayColor]];
    _emailField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.view addSubview:self.emailField];
    // 分割线
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,CGRectGetMaxY(_emailField.frame) + BASEWIDTH - 1, self.view.width - BASEWIDTH * 6, 1)];
    view3.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view3];
    
    //输入密码
    _passField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(view3.frame),CGRectGetMaxY(view3.frame) + BASEWIDTH - 1, CGRectGetWidth(view3.frame), BASEHEIGHT + 3)];
    _passField.placeholder = @"输入密码(6~15个字符)";
    _passField.keyboardType = UIKeyboardTypeEmailAddress;
    _passField.returnKeyType = UIReturnKeyNext;
    _passField.delegate = self;
    _passField.secureTextEntry = YES;
    _passField.tag = 10003;
    _passField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.passField setTextColor:[UIColor grayColor]];
    _passField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.view addSubview:self.passField];
    
    //分割线
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,CGRectGetMaxY(_passField.frame) + BASEWIDTH - 1, self.view.width - BASEWIDTH * 6, 1)];
    view4.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view4];
    
    //确认密码
    _surePField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(view4.frame),CGRectGetMaxY(view4.frame) + BASEWIDTH - 1, CGRectGetWidth(view4.frame), BASEHEIGHT + 3)];
    _surePField.placeholder = @"确认密码(6~15个字符)";
    _surePField.keyboardType = UIKeyboardTypeEmailAddress;
    _surePField.returnKeyType = UIReturnKeyNext;
    _surePField.delegate = self;
    _surePField.secureTextEntry = YES;
    _surePField.tag = 10004;
    _surePField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.surePField setTextColor:[UIColor grayColor]];
    _surePField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.view addSubview:self.surePField];
    // 分割线
    UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,CGRectGetMaxY(_surePField.frame) + BASEWIDTH - 1, self.view.width - BASEWIDTH * 6, 1)];
    view5.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view5];

    //创建账号
    UIButton *creat = [UIButton buttonWithType:UIButtonTypeSystem];
    creat.frame = CGRectMake(BASEWIDTH * 1.5, BASEHEIGHT * 22.275, BASEWIDTH * 15.75, BASEHEIGHT * 2);
    [creat setTitle:@"创 建 账 号" forState:UIControlStateNormal];
    [creat addTarget:self action:@selector(creatAction) forControlEvents:UIControlEventTouchUpInside];
    creat.tintColor = [UIColor whiteColor];
    [creat setBackgroundImage:[UIImage imageNamed:@"button@2x.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:creat];
    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
    login.frame = CGRectMake(BASEWIDTH * 1.5, CGRectGetMaxY(creat.frame) + BASEWIDTH * 0.8 , BASEWIDTH * 15.75, BASEHEIGHT * 2);
    [login setTitle:@"登  录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    login.tintColor = [UIColor colorWithRGB:0xf15b6c alpha:1];

    
    [self.view addSubview:login];

    // 验证
    UIButton *authentication = [UIButton buttonWithType:UIButtonTypeCustom];
    authentication.frame = CGRectMake(CGRectGetMaxX(view1.frame) - 90, CGRectGetMaxY(view1.frame) + 16, 90, 25);
    [authentication setBackgroundImage:[UIImage imageNamed:@"发送验证码@2x.png"] forState:UIControlStateNormal];
    [authentication setTitle:@"发送验证码" forState:UIControlStateNormal];
    [authentication addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    authentication.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [self.view addSubview:authentication];
    
    
    

}
- (void)leftBarAction{

    [self.navigationController popViewControllerAnimated:YES];

}
- (void)userInfoAction{
    
}
- (void)sendAction{
    NSLog(@"发送验证码");
    if (self.userField.text.length  != 11) {
                    [self.userField resignFirstResponder];
                    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    self.HUD.mode = MBProgressHUDModeText;
                    //常用的设置
                    //小矩形的背景色
                    self.HUD.color = [UIColor blackColor];//这儿表示无背景
                    self.HUD.alpha = 0.5;
                    self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
                    //显示的文字
                    self.HUD.labelText = @"请输入正确的手机号";
                    self.HUD.dimBackground = YES;
                    self.emailField.userInteractionEnabled = NO;
    }
    
}
- (void)creatAction{
    if (![self.surePField.text isEqualToString: self.passField.text]) {
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.mode = MBProgressHUDModeText;
        self.HUD.color = [UIColor blackColor];
        self.HUD.alpha = 0.5;
        self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
        self.HUD.labelText = @"两次输入的密码不一致";
        self.HUD.dimBackground = YES;
    }else{
    NSString *str = @"http://192.168.0.8:8083/user";
    //1.管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //2.设置登录参数
    NSDictionary *dict = @{@"username":self.userField.text, @"password":self.passField.text, @"email":self.emailField.text};
    
    //3.请求
    [manager POST:str parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"%@", responseObject);
        
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    }
    
}
- (void)loginAction{
    [self.navigationController popViewControllerAnimated:YES];

}
#pragma mark - touchMethod
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [super touchesBegan:touches withEvent:event];
    
    [self.userField resignFirstResponder];
    [self.passField resignFirstResponder];
    [self.emailField resignFirstResponder];
    [self.surePField resignFirstResponder];
    [self.HUD hide:YES];
    [self.HUD removeFromSuperview];
}



- (void)textFieldDidBeginEditing:(UITextField *)textField{
//    if (textField == self.emailField) {
//        if (self.userField.text.length  == 11) {
//            [self.userField resignFirstResponder];
//            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//            self.HUD.mode = MBProgressHUDModeText;
//            //常用的设置
//            //小矩形的背景色
//            self.HUD.color = [UIColor blackColor];//这儿表示无背景
//            self.HUD.alpha = 0.5;
//            self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
//            //显示的文字
//            self.HUD.labelText = @"请输入正确的手机号";
//            self.HUD.dimBackground = YES;
//            self.emailField.userInteractionEnabled = NO;
//            
//        }else{
//            NSString *str = [NSString stringWithFormat:@"http://192.168.0.8:8083/user/check/%@/1", self.userField.text];
//            [AFNTool get:str Body:nil HttpResult:JSON success:^(id result) {
//                NSDictionary *dic = result;
//                NSInteger * num = [dic[@"result"] integerValue];
//
//                if (!num) {
//                    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                    self.HUD.mode = MBProgressHUDModeText;
//                    //常用的设置
//                    //小矩形的背景色
//                    self.HUD.color = [UIColor blackColor];//这儿表示无背景
//                    self.HUD.alpha = 0.5;
//                    self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
//                    //显示的文字
//                    self.HUD.labelText = @"您的用户名已经被使用";
//                    self.HUD.dimBackground = YES;
//                  
//                    
//                }
//                
//            } failure:^(NSError *error) {
//                
//                NSLog(@"%@",error);
//            }];
//        }
//
//    }else if(textField == self.userField){
//        self.emailField.userInteractionEnabled = YES;
//    }else if(textField == self.passField){
//        
//        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
//        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
//        if (![emailTest evaluateWithObject:self.emailField.text]) {
//            [self.emailField resignFirstResponder];
//            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//            self.HUD.mode = MBProgressHUDModeText;
//            //常用的设置
//            //小矩形的背景色
//            self.HUD.color = [UIColor blackColor];//这儿表示无背景
//            self.HUD.alpha = 0.5;
//            self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
//            //显示的文字
//            self.HUD.labelText = @"邮箱格式不正确";
//            self.HUD.dimBackground = YES;
//        }else{
//            NSString *str = [NSString stringWithFormat:@"http://192.168.0.8:8083/user/check/%@/3", self.emailField.text];
//            [AFNTool get:str Body:self HttpResult:JSON success:^(id result) {
//                NSDictionary *dic = result;
//                NSInteger * num = [dic[@"result"] integerValue];
//                
//                if (!num) {
//                    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                    self.HUD.mode = MBProgressHUDModeText;
//                    //常用的设置
//                    //小矩形的背景色
//                    self.HUD.color = [UIColor blackColor];//这儿表示无背景
//                    self.HUD.alpha = 0.5;
//                    self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
//                    //显示的文字
//                    self.HUD.labelText = @"您的邮箱已被注册";
//                    self.HUD.dimBackground = YES;
//                }
//                
//            } failure:^(NSError *error) {
//                
//                NSLog(@"%@",error);
//            }];
//        }
//    }else
    if(textField == self.surePField){
        if (self.passField.text.length < 6) {
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.HUD.mode = MBProgressHUDModeText;
            self.HUD.color = [UIColor blackColor];
            self.HUD.alpha = 0.5;
            self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
            self.HUD.labelText = @"密码过短";
            self.HUD.dimBackground = YES;
            
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 点击Done隐藏键盘
    if (textField == self.surePField) {
        
        [textField resignFirstResponder];
        
    } else if (textField == self.userField)
    {
        // 点击next跳到下一个输入框
        [self.userField resignFirstResponder];
        [self.emailField becomeFirstResponder];
        
    }else if(textField == self.emailField){
        [self.emailField resignFirstResponder];
        [self.passField becomeFirstResponder];
        
    }else if (textField == self.passField){
        [self.passField resignFirstResponder];
        [self.surePField becomeFirstResponder];
    }
    return YES;
    
}


@end
